/*
* @Author: Administrator
* @Date:   2019-08-18 11:56:49
* @Last Modified by:   Administrator
* @Last Modified time: 2019-08-18 14:27:18
*/
var express = require('express');
var path = require('path');

var router = express.Router();

var options = {
	root: __dirname + '/../templates/',

}

var multer  = require('multer')

var storage = multer.diskStorage({
  // 可以动态处理文件的保存路径
  destination: function (req, file, cb) {
    cb(null, path.resolve(__dirname, '../uploads/'))
  },
  // 动态的处理保存的文件名
  filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname)) 
    // 这里的关键是这个时间戳，能保证文件名的唯一性（不严谨）
  }
})

var upload = multer({ storage: storage })




var indexController = require('../controllers/index.js');
var newsController = require('../controllers/news.js');

var bodyParser = require('body-parser')

var urlencodedParser = bodyParser.urlencoded({ extended: false })


router
	.get('/', function (request, response) {
		
		response.render('index.html');

	})
	.get('/about', function (request, response) {

		response.render( 'about.html');

	})
	.get('/profile', function (request, response) {
		if(!request.session.userInfo){
			response.redirect('/login');
			return;
		}
		response.render('profile.html');

	}).get('/news', newsController.lst)

	.get('/news/:id', function (request, response) {
		console.log( request.params.id );
		var id = request.params.id;
		
		var infos = {
			id,
			title: 'Lorem ipsum dolor sit amet, consectetur.',
			infos: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maiores explicabo autem earum illo minus quaerat temporibus veniam repellat corporis animi sit laborum voluptate atque, eos perferendis. Aliquam distinctio dolores, aperiam.',
			date: '2019年8月18日12:48:36',
			author: '新华社'
		}
		// response.status(200).sendFile(path.resolve(__dirname, '../templates/', 'detail.html'));
		response.status(200).render('detail.html', {infos});

	})
	.get('/json', indexController.showIndex)
	.get('/login', indexController.login)
	.get('/logout', indexController.logout)
	.post('/login', upload.single('avatar'), indexController.loginCheck)

module.exports = router